package shooting.ui.layer
{
	import __AS3__.vec.Vector;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import shooting.ui.IObject;
	import shooting.ui.IObjectContainer;
	
	import utils.Library;

	public class BitmapLayer extends Bitmap
	{
		private var objectDic:Dictionary;
		private var library:Library;
		private var layerRect:Rectangle;
		private var layerPoint:Point;
		private var layerBitmapData:BitmapData;
		private var layerColor:Number;
		private var objectContainer:IObjectContainer = null;
		
		public function BitmapLayer(m_lib:Library)
		{
			super();
			objectDic = new Dictionary();
			library = m_lib;
		}
		/**
		 * 스테이지 초기화
		 **/
		public function initLayer( layerWidth:Number, layerHeight:Number, transparent:Boolean = true, color:uint = 0 ):void
		{
			layerRect = new Rectangle(0, 0, layerWidth, layerHeight );
			layerPoint = new Point( 0, 0 );
			layerColor = color;
			layerBitmapData = new BitmapData( layerWidth, layerHeight, transparent, color);
			
			this.bitmapData = layerBitmapData;
		}
		public function setContainer(container:IObjectContainer):void
		{
			objectContainer = container;
		}
		/**
		 * 랜더링
		 **/
		public function render( isAfterimage:Boolean = false ):void
		{
			layerBitmapData.lock();
			//unitManager.render();
			if(!isAfterimage)
			{
				layerBitmapData.fillRect( layerRect, layerColor );
			}
			var list:Array = objectContainer.getList();
			for each( var obj:IObject in list )
			{
				var msBmp:BitmapData = getObjectBMP(obj.kind ,obj.dirction);
				//layerBitmapData.copyPixels( msBmp, msBmp.rect, new Point(), null, null, true );
				var msPosition:Point = obj.getPosition();
				 msPosition = msPosition.clone();
				msPosition.offset(-msBmp.width/2, -msBmp.height/2); 
				layerBitmapData.copyPixels( msBmp, msBmp.rect, msPosition, null, null, true );
			}
			layerBitmapData.unlock();
		}
		private function getObjectBMP(name:String, rotation:Number = 0):BitmapData
		{
			if(objectDic[name] == null)
			{
				objectDic[name] = new Vector.<BitmapData>(40,false);
			}
			var temRot:uint = (fixRotation(rotation)%(Math.PI*2)) / Math.PI * 20;
			if(objectDic[name][temRot] == null)
			{
				var orgBmp:BitmapData = library.getBitmap(name).bitmapData;
				var wh:Number = Point.distance(new Point(), new Point(orgBmp.width,orgBmp.height));
				var bmp:BitmapData = new BitmapData(wh, wh,true, 0);
				var matrix:Matrix = new Matrix();
				matrix.translate( -wh/2,-wh/2);
				matrix.rotate( temRot * Math.PI / 20);
				matrix.translate( wh/2,wh/2);
				bmp.draw( orgBmp, matrix );
				objectDic[name][temRot] = bmp;
			} 
			return objectDic[name][temRot];
		}
		private function fixRotation(r:Number):Number
		{
			var retN:Number = r;
			for(;retN<0;)
			{
				retN += Math.PI*2;
			}
			return retN;
		}
	}
}